﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;
using UNDP_Projects.Classes;

//Used to send an email
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace UNDP_Projects.Account
{
    public partial class Forgot : Security
    {
        UNDPEntities db = new UNDPEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GetPWordButton_Click(object sender, EventArgs e)
        {
            string enteredEmail = EmailTX.Text.Trim();
            string thePassword = null;

            try
            {
                string pWord = (from data in db.users
                                where
                                    data.user_email == enteredEmail
                                select data.password).FirstOrDefault();

                thePassword = base64Decode(pWord);
            }
            catch (Exception ex)
            {
                //throw new Exception("Login Error "+ex.Message+" "+ex.StackTrace);
                Error.Visible = true;
                Error.Text = "DATABASE ERROR: " + ex.Message+"<br />"+ex.StackTrace;
            }

            // If no password found, return message.
            if (thePassword == null)
            {
                Error.Visible = true;
                Error.Text = "That Email address is not in the database. Please contact the System Administrator.";
                return;
            }

            //===========================================+
            //  Send Email Message to Welcome People     |
            //===========================================+

            //Create the mail message
            MailMessage mail = new MailMessage();

            //Set the addresses and specify a friendly 'from' name
            mail.From = new MailAddress("monitoring.so@undp.org", "M&E");
            const string fromPassword = "Monitoring2012";

            //Add multiple addresses
            mail.To.Add(new MailAddress(enteredEmail));

            //Set the content
            mail.Subject = "Monitoring and Evaluation Reporting System User Password";

            System.Text.StringBuilder sbuilder = new System.Text.StringBuilder();
            sbuilder.Append("Your current login details are:").AppendLine().AppendLine();
            sbuilder.Append("Username: " + enteredEmail).AppendLine();
            sbuilder.Append("Default Password: " + thePassword).AppendLine().AppendLine();
            sbuilder.Append("You can access the system here: http://www.ppu-reporting.net:8083/ ").AppendLine().AppendLine();
            sbuilder.Append("Thank you").AppendLine().AppendLine();
            sbuilder.Append("M&E team").AppendLine().AppendLine().AppendLine();
            sbuilder.Append("*This email is automatically generated - please do not reply").AppendLine();

            mail.Body = sbuilder.ToString();
            mail.Priority = MailPriority.Normal;
            //Send the message
            var smtp = new SmtpClient
            {
                Host = "193.194.138.69",
                Port = 25,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(mail.From.Address, fromPassword)
            };

            ServicePointManager.ServerCertificateValidationCallback = delegate(
                    object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };

            try
            {
                smtp.Send(mail);
                Error.Text = "Please check your inbox for your password.";
                Error.Visible = true;
                EmailTX.Text = string.Empty;
            }
            catch (Exception ex)
            {
                Error.Text = "MSG NOT SENT " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
            //=============================================+
            //                  END SEND MAIL              |
            //=============================================+
        }
    }
}